var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
      order_num:"",
      refund_id:"0",
      refund_qty:"0",
      array: [],
      index: 0,
      bg_status: "none",
      info:""
  },


  getOrderDetail(order_num) {
    let that=this;
    app.getUserToken(function (err, token) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.globalData.url +'/index.php?_m=checkout/order/get_item&order_num=' + order_num,
        data: { auth_token: token },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.getOrderDetail();
          } else if (res.data.code == 0) {
            let url = "https://m.zhcxkj.com/qrpu?c=" + res.data.data.c + "&id=" + res.data.data.id;
            let arr=[];
            res.data.data.refund_reason.forEach(function (item, index) {
              arr[index] = item.label_cn
            })
  
            that.setData({
              info:res.data.data,
              array:arr
            })
          }
        }
      })
    })

  },

  onShow:function(){
    let order_num = this.data.order_num
    this.getOrderDetail(order_num)
  },
 

  onLoad: function (options) {
    this.setData({
      order_num: options.order_num
    })
    this.getOrderDetail(options.order_num);
  
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })

  },
  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  },

  refund:function(e){
    let refund_id = e.currentTarget.dataset.id;
    let refund_qty = e.currentTarget.dataset.qty;
    this.setData({
      refund_id: refund_id,
      refund_qty: refund_qty,
      bg_status: "block"
    })
  },

  refundSubmit: function (e) {
    let refund_id = e.detail.value.refund_id;
    let refund_qty = e.detail.value.refund_qty;
    let refund_reason = e.detail.value.refund_reason;
    let refund_remark = e.detail.value.refund_remark;
    let old_qty = this.data.refund_qty;
    let that = this;
    if (refund_qty > old_qty){
      wx.showModal({
        title: "提示",
        content: '请输入正确的数量'
      })
      return false;
    }
    app.getUserToken(function (err, token) {
      wx.request({
        url: app.globalData.url +'index.php?_m=checkout/order/apply_refund',
        data: { refund_id: refund_id, refund_reason: refund_reason, refund_qty: refund_qty, refund_remark: refund_remark, auth_token: token },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },

        method: 'POST',
        success: (res) => {
          if (res.data.code == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.add_cart();
          } else if (res.data.code == 0) {
            wx.showModal({
              title: '退款说明',
              content: res.data.msg,
              success(res) {
                that.setData({
                  bg_status: "none"
                })
                let order_num = that.data.order_num
                that.getOrderDetail(order_num)
              }
            })
          } else {
            wx.showModal({
              title: "提示",
              content: res.data.msg
            })
            return false;
          }
        }
      })
    })

  },

  closeEdit:function(e){
    this.setData({
      bg_status: "none"
    })
  },

  wxpay: function (e) {
    let order_num = this.data.order_num;
    var that = this;

    app.getUserToken(function (err, token) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.globalData.url +'index.php?_m=checkout/wxpay&order_num=' + order_num,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        data: { auth_token: token },
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.wxpay();
          } else if (res.data.code == 0) {
            console.log(res.data.data.order_num);
          
            let orderId = res.data.data.order_num;
            let money = (parseFloat(that.data.info.order_total) * 100).toFixed(0);
            that.thirdPay(orderId, money);

          }
        },
        fail: (res) => {
          wx.showToast({
            title: '获取借口失败',
            duration: 2000
          })
        }
      })

    })

  },

  thirdPay: function (orderId, money) {
    var that = this;
    wx.cloud.callFunction({
      name: "pay",
      data: {
        orderId: orderId,
        money: money
      },
      success(res) {
        console.log("获取成功", res)
       
        that.goPay(res.result, orderId);
      },
      fail(err) {
        console.log("获取失败", err)
      }
    })
  },


  //微信支付
  goPay(payData, orderId) {
    wx.requestPayment({
      timeStamp: payData.timeStamp,
      nonceStr: payData.nonceStr,
      package: payData.package,
      signType: 'MD5',
      paySign: payData.paySign,
      success(res) {
        console.log('orderId:' + orderId);
        wx.redirectTo({
          url: "../success/success?order_num=" + orderId
        })

      },
      fail(res) {
        console.log("支付失败", res)
      }
    })
  },



})